package sanshui.system.trade.factory.trade;

import lombok.Data;
import sanshui.system.trade.common.GlobalContain;
import sanshui.system.trade.db.entity.TradeLimitOrder;
import sanshui.system.trade.factory.data.*;
import sanshui.system.trade.factory.distribute.DistributeManager;
import sanshui.system.trade.factory.timeManager.TimeManager;
import sanshui.system.trade.factory.trade.indicator.IndicatorResult;
import sanshui.system.trade.factory.trade.riskCtr.IRiskStrategy;
import sanshui.system.trade.factory.trade.strategy.StrategyResult;
import sanshui.system.trade.factory.trade.strategy.TradeStrategy;
import sanshui.system.trade.model.*;
import sanshui.system.trade.util.CommonTimeUtil;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Data
public class TradeSystemContext {
    Long accId;
    Long workerId;
    Long userId;
    String tradeDate;
    // 交易系统当前驱动时间
    String tradeTime;
    TradeStrategy strategy; // 交易信号策略
    IRiskStrategy riskStrategy; // 风控策略
    TradeAccountModel account;
    DataCache dataCache;
    DistributeManager distributeManager;
    GlobalContain.NotifyChannel notifyChannel = GlobalContain.NotifyChannel.Push_Plus; //通知方式
    List<TradeHisPositionModel> positionModelList = new ArrayList<>(); // 标的交易历史信息
    List<StrategyResult> strategyResults = new ArrayList<>();
    List<StrategyResult> strategyRiskResults = new ArrayList<>();
    /*
     * 需要实时更新的上下文
     */
    Map<String, PositionAccModel> holdPositionMap = new HashMap<>(); // 持有标的信息

    Map<String, List<TradeLimitOrderModel>> limitOrderMap = new HashMap<>();

    List<RiskModel> riskModelList = new ArrayList<>();
    // 时间驱动管理器
    TimeManager timeManager;
    // 交易系统类型：实时、回测
    TradeSystemType tradeSystemType;
    // 交易市场：A股、OKEX
    TradeMarketKey tradeMarketKey;
    // 交易市场的数据类型: T0\T1
    DataMarketType dataMarketType;

    public void updateTradeDate(LocalDateTime currentTime) {
        tradeDate = CommonTimeUtil.getNowTime8(currentTime);
        tradeTime = CommonTimeUtil.getNowTime16(currentTime);
    }

    public void addPosInHisList(TradeHisPositionModel hisPos){
        if (this.getPositionModelList().size() < 100){
            this.getPositionModelList().add(hisPos);
        } else {
            this.getPositionModelList().remove(0);
            this.getPositionModelList().add(hisPos);
        }
    }

    public void clear(){
        positionModelList = null;
        holdPositionMap = null;
        if (dataCache != null){
            dataCache.clearDeep();
        }
    }
}
